﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using DBProvider;
using DBProvider.Providers;

namespace HealthCommunity.Controls
{
	public partial class UCSurveyDetail : System.Web.UI.UserControl
	{
		private ISurveyProvider _SurveyProvider;

		private Dictionary<int, List<SurveyQuestionItem>> _DicSrvQstnsSrvQstnItems;
		private int _CurrentSurveyQuestionIndex;

		public int CurrentSurveyID
		{
			get
			{
				return CommonHelper.QueryStringInt("surveyid");
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				BindSurvey();
			}
		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			var user = SiteContext.Current.CurrentUser;

			if (user == null)
			{
				return;
			}

			List<SurveyAnswer> lstSurveyAnswer = new List<SurveyAnswer>();

			if (_SurveyProvider == null)
			{
				_SurveyProvider = new SurveyProvider();
			}

			var lstSurveyQuestions = _SurveyProvider.GetSurveyQuestionBySurvey(CurrentSurveyID);

			foreach (RepeaterItem questionItem in rptSurveyQuestion.Items)
			{
				var hfSurveyQuestionID = questionItem.FindControl("hfSurveyQuestionID") as HiddenField;
				var rbtnlSurveyQuestionItem = questionItem.FindControl("rbtnlSurveyQuestionItem") as RadioButtonList;
				var chblSurveyQuestionItem = questionItem.FindControl("chblSurveyQuestionItem") as CheckBoxList;
				var txtSurveyQuestionItem = questionItem.FindControl("txtSurveyQuestionItem") as TextBox;

				var surveyQuestionID = int.Parse(hfSurveyQuestionID.Value);

				var theSurveyQuestion = lstSurveyQuestions.SingleOrDefault(sq => sq.SurveyQuestionID == surveyQuestionID);

				if (theSurveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.SingleChoise)
				{
					var theNewSurveyAnswer = new SurveyAnswer() { SurveyAnswerUserID = user.UserID, SurveyQuestionID = surveyQuestionID };
					theNewSurveyAnswer.SurveyAnswerSelectedQuestionItemID = int.Parse(rbtnlSurveyQuestionItem.SelectedValue);

					lstSurveyAnswer.Add(theNewSurveyAnswer);
				}
				else if (theSurveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.MultiChoise)
				{
					foreach (ListItem item in chblSurveyQuestionItem.Items)
					{
						if (item.Selected)
						{
							var theNewSurveyAnswer = new SurveyAnswer() { SurveyAnswerUserID = user.UserID, SurveyQuestionID = surveyQuestionID };
							var surveyQuestionItemID = int.Parse(item.Value);
							theNewSurveyAnswer.SurveyAnswerSelectedQuestionItemID = surveyQuestionItemID;
							lstSurveyAnswer.Add(theNewSurveyAnswer);
						}
					}
				}
				else
				{
					var theNewSurveyAnswer = new SurveyAnswer() { SurveyAnswerUserID = user.UserID, SurveyQuestionID = surveyQuestionID };
					theNewSurveyAnswer.SurveyAnswerContent = txtSurveyQuestionItem.Text.Trim();

					lstSurveyAnswer.Add(theNewSurveyAnswer);
				}
			}

			if (lstSurveyAnswer.Count > 0)
			{
				if (!_SurveyProvider.SaveSurveyAnswers(lstSurveyAnswer))
				{
					ltrMsg.Text = "提交时遇到错误";
				}
				else
				{
					Response.Redirect("~/SurveyList.aspx");
				}
			}
		}

		protected void rptSurvQstnItm_R_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null && e.Item.ItemIndex == 0)
			{
				var rbSurvQstnItem = e.Item.FindControl("rbSurvQstnItem") as RadioButton;
				rbSurvQstnItem.InputAttributes.Add("validate", "required:true");
				rbSurvQstnItem.InputAttributes.Add("name", "survqstnitm" + _CurrentSurveyQuestionIndex.ToString());
				//rbSurvQstnItem.Attributes["name"] = "survqstnitm" + _CurrentSurveyQuestionIndex.ToString();
			}
		}

		protected void rptSurvQstnItm_C_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null && e.Item.ItemIndex == 0)
			{
				var cbSurvQstnItem = e.Item.FindControl("cbSurvQstnItem") as CheckBox;
				cbSurvQstnItem.InputAttributes.Add("validate", "required:true");
				cbSurvQstnItem.InputAttributes.Add("name", "survqstnitm" + _CurrentSurveyQuestionIndex.ToString());
				//cbSurvQstnItem.Attributes["name"] = "survqstnitm" + _CurrentSurveyQuestionIndex.ToString();
			}
		}

		protected void rptSurveyQuestion_ItemDataBound(object sender, RepeaterItemEventArgs e)
		{
			if (e.Item.DataItem != null)
			{
				var currentSurveyQuestion = e.Item.DataItem as SurveyQuestion;

				if (currentSurveyQuestion != null)
				{
					var ltrSurveyQuestionType = e.Item.FindControl("ltrSurveyQuestionType") as Literal;
					var rbtnlSurveyQuestionItem = e.Item.FindControl("rbtnlSurveyQuestionItem") as RadioButtonList;
					var chblSurveyQuestionItem = e.Item.FindControl("chblSurveyQuestionItem") as CheckBoxList;
					var txtSurveyQuestionItem = e.Item.FindControl("txtSurveyQuestionItem") as TextBox;
					var hfSurveyQuestionID = e.Item.FindControl("hfSurveyQuestionID") as HiddenField;
					//var rptSurvQstnItm_R = e.Item.FindControl("rptSurvQstnItm_R") as Repeater;
					//var rptSurvQstnItm_C = e.Item.FindControl("rptSurvQstnItm_C") as Repeater;

					hfSurveyQuestionID.Value = currentSurveyQuestion.SurveyQuestionID.ToString();

					if (currentSurveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.SingleChoise)
					{
						ltrSurveyQuestionType.Text = "单选";
						chblSurveyQuestionItem.Visible = false;
						txtSurveyQuestionItem.Visible = false;
						rbtnlSurveyQuestionItem.Visible = true;

						if (_DicSrvQstnsSrvQstnItems != null && _DicSrvQstnsSrvQstnItems.Count > 0 && _DicSrvQstnsSrvQstnItems.Keys.Contains(currentSurveyQuestion.SurveyQuestionID))
						{
							_CurrentSurveyQuestionIndex = e.Item.ItemIndex;

							rbtnlSurveyQuestionItem.DataSource = _DicSrvQstnsSrvQstnItems[currentSurveyQuestion.SurveyQuestionID];
							rbtnlSurveyQuestionItem.DataBind();


						}
					}
					else if (currentSurveyQuestion.SurveyQuestionType == (int)SurveyQuestionType.MultiChoise)
					{
						ltrSurveyQuestionType.Text = "多选";
						chblSurveyQuestionItem.Visible = true;
						txtSurveyQuestionItem.Visible = false;
						rbtnlSurveyQuestionItem.Visible = false;

						if (_DicSrvQstnsSrvQstnItems != null && _DicSrvQstnsSrvQstnItems.Count > 0 && _DicSrvQstnsSrvQstnItems.Keys.Contains(currentSurveyQuestion.SurveyQuestionID))
						{
							_CurrentSurveyQuestionIndex = e.Item.ItemIndex;

							chblSurveyQuestionItem.DataSource = _DicSrvQstnsSrvQstnItems[currentSurveyQuestion.SurveyQuestionID];
							chblSurveyQuestionItem.DataBind();
						}
					}
					else
					{
						ltrSurveyQuestionType.Text = "填空";
						chblSurveyQuestionItem.Visible = false;
						txtSurveyQuestionItem.Visible = true;
						rbtnlSurveyQuestionItem.Visible = false;
					}
				}
			}
		}

		private void BindSurvey()
		{
			if (CurrentSurveyID > 0)
			{
				if (_SurveyProvider == null)
				{
					_SurveyProvider = new SurveyProvider();
				}

				var theSurvey = _SurveyProvider.GetSurvey(CurrentSurveyID);

				if (theSurvey != null)
				{
					ltrSurveyTitle.Text = theSurvey.SurveyTitle;
					ltrSurveyDescription.Text = theSurvey.SurveyDescription;

					var surveyQuestions = _SurveyProvider.GetSurveyQuestionBySurvey(CurrentSurveyID);

					var surveyQuestionItems = _SurveyProvider.GetSurveyQuestionItemBySurvey(CurrentSurveyID);

					if (surveyQuestions != null)
					{
						_DicSrvQstnsSrvQstnItems = new Dictionary<int, List<SurveyQuestionItem>>();

						foreach (var question in surveyQuestions)
						{
							var items = surveyQuestionItems.Where(sqi => sqi.SurveyQuestionID == question.SurveyQuestionID);

							if (items != null)
							{
								_DicSrvQstnsSrvQstnItems.Add(question.SurveyQuestionID, items.ToList());
							}
						}
					}

					rptSurveyQuestion.DataSource = surveyQuestions;
					rptSurveyQuestion.DataBind();
				}
			}
		}
	}
}